[app-engine-java-groovy] One-to-Many relationship. Select objects from datastore.

Posted by Olexandr on Stack Overflow See other posts from Stack Overflow or by Olexandr
Published on 2009-09-30T16:09:02Z Indexed on 2010/04/24 16:53 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

Hi. I've omitted some code(package declarations, imports, other fields) for shortness. I have here simple One-to-Many relation. It worked fine till this moment.

@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
class Restaurant implements Serializable {

 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 Key id

 @Persistent(mappedBy = "restaurant")
 List<RestaurantAddress> addresses = new ArrayList<RestaurantAddress>()
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
class RestaurantAddress implements Serializable {

 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 Key id

 @Persistent
 Restaurant restaurant
}

Now i need to get(select) all the Restaurants from DB:

def getRestaurantsToExport(final String dst, final int count) {
   String field = restaurantExportFields[dst]
   return transactionExecute() { PersistenceManager pm ->
     Query q = pm.newQuery(Restaurant.class)
     q.filter = "$field == null"
     q.setRange(0, count)
     return q.execute()
   }
 }

But there are on problem - query gives me 12 restaurants(as in DB) but every Restaurant has 0 Address but in Datastore every Restaurant has minimum 2 addresses.

Have anyone the same problem or knows the solution ?

© Stack Overflow or respective owner

Related posts about java

Related posts about groovy